package com.jhd.fuzhou.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.util.BusinessStatus;
import com.jhd.fuzhou.view.daily.DailyCompanyDetailActivity;
import com.jhd.fuzhou.view.daily.DailyStepOneActivity;
import com.jhd.fuzhou.view.daily.DailySupervisionActivity;
import com.jhd.fuzhou.vo.SuperviseEnclosureVo;
import com.jhd.fuzhou.vo.SupervisedUnitVo;

import java.util.List;

public class UnitAdapter extends RecyclerView.Adapter<UnitAdapter.ViewHolder> {
    private List<SupervisedUnitVo> supervisedUnitVoList;private Context context;
    @Override
    public UnitAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_company, parent, false);
        UnitAdapter.ViewHolder viewHolder = new UnitAdapter.ViewHolder(itemView);
        return viewHolder;
    }
    public UnitAdapter(List<SupervisedUnitVo> supervisedUnitVoList, Context context) {
        this.supervisedUnitVoList = supervisedUnitVoList;
        this.context = context;
    }

    @Override
    public void onBindViewHolder(@NonNull UnitAdapter.ViewHolder holder, int position) {
        holder.tv_legalRepresentative.setText(supervisedUnitVoList.get(position).getLegalRepresentative());
        BusinessStatus.setText(holder.tv_business_status,supervisedUnitVoList.get(position).getBusinessStatus());
        holder.tv_speciality.setText(supervisedUnitVoList.get(position).getSpecialityCategory() + " - " + supervisedUnitVoList.get(position).getSpecialityDetail());
        if (supervisedUnitVoList.get(position).getRegisterAddress() != null) {
            holder.unitAddress.setText(supervisedUnitVoList.get(position).getRegisterAddress());
        } else {
            holder.unitAddress.setText(supervisedUnitVoList.get(position).getRegisterProvince() + supervisedUnitVoList.get(position).getRegisterCity());
        }
        holder.tv_unitName.setText(supervisedUnitVoList.get(position).getUnitName());
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, DailyCompanyDetailActivity.class);
                //加入参数，传递给AnotherActivity
                Gson gson = new Gson();
                intent.putExtra("data", gson.toJson(supervisedUnitVoList.get(position)));
                context.startActivity(intent);
            }
        });
        holder.btn_add_report.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, DailyStepOneActivity.class);
                Gson gson = new Gson();
                intent.putExtra("data", gson.toJson(supervisedUnitVoList.get(position)));
                context.startActivity(intent);
            }
        });
        if(supervisedUnitVoList.get(position).equals("A")){
            holder.iv_grade.setBackgroundResource(R.mipmap.ic_grade_a);
        }else if(supervisedUnitVoList.get(position).equals("B")){
            holder.iv_grade.setBackgroundResource(R.mipmap.ic_grade_b);
        }else if(supervisedUnitVoList.get(position).equals("C")){
            holder.iv_grade.setBackgroundResource(R.mipmap.ic_grade_c);
        }else{
            holder.iv_grade.setBackgroundResource(R.mipmap.ic_grade_no);
        }
    }

    @Override
    public int getItemCount() {
        return supervisedUnitVoList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_legalRepresentative, tv_speciality, unitAddress, tv_unitName,tv_business_status; //法人，所属专业，地址,被检测单位
        ImageView iv_grade; //等级
        Button btn_add_report;
        LinearLayout linearLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            this.tv_legalRepresentative = itemView.findViewById(R.id.tv_legalRepresentative);
            this.tv_speciality = itemView.findViewById(R.id.tv_speciality);
            this.unitAddress = itemView.findViewById(R.id.unitAddress);
            this.tv_unitName = itemView.findViewById(R.id.tv_unitName);
            this.iv_grade = itemView.findViewById(R.id.iv_grade);
            this.btn_add_report = itemView.findViewById(R.id.btn_add_report);
            this.tv_business_status=itemView.findViewById(R.id.tv_business_status);
            this.linearLayout = itemView.findViewById(R.id.linearLayout);
        }
    }
}

